package com.lagou.edu.interceptor;

import com.lagou.edu.common.Constant;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class SecurityIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (Objects.nonNull(session)){
            Object attribute = session.getAttribute(Constant.CURRENT_USER);
            if (Objects.nonNull(attribute)){
                return true;
            }
        }
        response.sendRedirect("/auth/login?error=UNAUTHORIZED");
        return false;
    }
}
